﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data.MySqlClient;
using MySql.Data;
using System.Data;
using System.Configuration;

namespace ChuanPlus
{
    class DataProvider
    {
        public static DataTable execute(string sql, MySqlParameter[] param)
        {
            try
            {
                AppSettingsReader config = new AppSettingsReader();
                sql = ((string)(config.GetValue("Database", typeof(string)))) + "." + sql;
                MySqlCommand command = new MySqlCommand(sql, new MySqlConnection(ChuanPlus.Properties.Settings.Default.ChuanPlusConnectionString));
                command.CommandType = System.Data.CommandType.StoredProcedure;
                if (param != null)
                    foreach (MySqlParameter p in param)
                    {
                        command.Parameters.Add(p);
                    }
                MySqlDataAdapter adap = new MySqlDataAdapter(command);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static DataTable executeQuery(string sql, MySqlParameter[] param)
        {
            AppSettingsReader config = new AppSettingsReader();            
            MySqlCommand command = new MySqlCommand(sql, new MySqlConnection(ChuanPlus.Properties.Settings.Default.ChuanPlusConnectionString));
            command.CommandType = System.Data.CommandType.Text;
            if (param != null)
                foreach (MySqlParameter p in param)
                {
                    command.Parameters.Add(p);
                }
            MySqlDataAdapter adap = new MySqlDataAdapter(command);
            DataSet ds = new DataSet();
            adap.Fill(ds);
            return ds.Tables[0];
        }
    }
}
